<?php

class friends_model_invitation extends Model {
	public function __construct(){
		parent::__construct();
		$this->table = "cartoon_friends_invitation";
	}
	
	public function gencode($uid, $username){
		$sql = "insert ignore into ".$this->table." (id, username, code, create_time, update_time) 
values(&uid, &username, &code, &create_time, &update_time)";
		$params = array(
			'uid'=>$uid,
			'username'=>$username,
			'code'=>uniqid(),
			'create_time'=>$_SERVER['REQUEST_TIME'],
			'update_time'=>$_SERVER['REQUEST_TIME']
		);
		if($this->db->query($sql, $params)){
			return $params['code'];
		}
		return false;
	}
	
	public function setTxt($uid, $txt){
		$sql = "update ".$this->table." set txt=&txt, update_time=&update_time where id=&uid";
		$params = array('uid'=>$uid, 'txt'=>$txt, 'update_time'=>$_SERVER['REQUEST_TIME']);
		return $this->db->query($sql, $params);
	}
	
	public function select($uid, $fields='*'){
		$sql = "select $fields from ".$this->table." where id=&uid";
		$params = array('uid'=>$uid);
		return $this->db->fetchRow($sql, $params);
	}
	
	public function setClose($uid, $closed){
		$sql = "update ".$this->table." set closed=&closed, update_time=&update_time where id=&uid";
		$params = array(
			'uid'=>$uid, 'closed'=>$closed, 'update_time'=>$_SERVER['REQUEST_TIME']
		);
		return $this->db->query($sql, $params);
	}
	
	public function log($host_id, $uid){
		$sql = "insert into app_friends_invitation_logs (host_id, uid, create_time) values(&host_id, &uid, &create_time)";
		$params = array('host_id'=>$host_id, 'uid'=>$uid, 'create_time'=>$_SERVER['REQUEST_TIME']);
		return $this->db->query($sql, $params);
	}
	
	public function checkQuest($uid, $time){
		$sql = "select count(*) c from app_friends_invitation_logs where host_id=&uid and create_time>&time";
		$params = array('uid'=>$uid, 'create_time'=>$time);
		return $this->db->fetchField($sql, 'c', $params);
	}
	
}